package com.nulstudio.sppm.controller;
import com.nulstudio.sppm.entity.Commodity;
import com.nulstudio.sppm.entity.Customer;
import com.nulstudio.sppm.network.*;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.*;
import java.util.stream.Collectors;

@Controller
@CrossOrigin(origins = "*")
@RequestMapping("/service/commodity")
public final class CommodityController {

    private final List<Commodity> commodityList = new ArrayList<>();
    @RequestMapping("/fetchAll")
    @ResponseBody
    public Result<Commodity[]> fetchAll(@RequestBody Request<?> request) {
        final Commodity[] commodities = new Commodity[commodityList.size()];
        for (int i = 0; i < commodityList.size(); ++i) {
            commodities[i] = commodityList.get(i);
        }
        return new Result<>(commodities);
    }

    @RequestMapping("/createEntry")
    @ResponseBody
    public Result<CreateCommodityResponse> createEntry(@RequestBody Request<CreateCommodityRequest> rawRequest) {
        final CreateCommodityRequest request = rawRequest.getRequest();
        if (commodityList.stream().filter(customer -> customer.getName().equals(request.getName()))
                .findFirst().orElse(null) != null) {
            return new Result<>(100, "Fuck");
        }
        final int id = commodityList.isEmpty() ? 1 : commodityList.get(commodityList.size() - 1).getId() + 1;
        final Commodity commodity = new Commodity(id, request.getName(), request.getNotes(),
                request.getRetailPrice(), request.getCostPrice(), request.getWholesalePrice());
        commodityList.add(commodity);
        return new Result<>(new CreateCommodityResponse(id));
    }

    @RequestMapping("/modifyEntry")
    @ResponseBody
    public Result<?> modifyEntry(@RequestBody Request<Commodity> request) {
        final Commodity commodity = commodityList.stream()
                .filter(item -> item.getId() == request.getRequest().getId())
                .findFirst().orElse(null);
        if (commodity == null) {
            return new Result<>(100, "Fuck");
        }
        commodityList.remove(commodity);
        commodityList.add(request.getRequest());
        commodityList.sort(Comparator.comparing(Commodity::getId));
        return new Result<>();
    }

    @RequestMapping("/deleteEntry")
    @ResponseBody
    public Result<?> deleteEntry(@RequestBody Request<DeleteCommodityRequest> request) {
        final Set<Integer> deleteIds = new HashSet<>(Arrays.asList(request.getRequest().getId()));
        final List<Commodity> commodities = commodityList.stream()
                .filter(item -> !deleteIds.contains(item.getId()))
                .collect(Collectors.toList());
        commodityList.clear();
        commodityList.addAll(commodities);
        return new Result<>();
    }
}
